package com.example.mydemo.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.mydemo.entity.UserRole;
import com.example.mydemo.entity.domain.MenuVO;
import com.example.mydemo.mapper.ClockMapper;
import com.example.mydemo.mapper.UserRoleMapper;
import com.example.mydemo.mapper.UsersMapper;
import com.example.mydemo.model.UserVO;
import com.example.mydemo.model.loginVO;
import com.example.mydemo.entity.Users;
import com.example.mydemo.service.IUserRoleService;
import com.example.mydemo.service.UserService;
import com.example.mydemo.util.RedisUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public  class UserServiceImpl extends ServiceImpl<UsersMapper, Users> implements UserService {

    @Autowired
    private UsersMapper userMapper;
    @Autowired
    private ClockMapper dakaMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserRoleMapper userRoleMapper;
//    @Value("${kevinq.name}")
//    private String name;
//    @Value("${debug.value}")
//    private boolean debug;
    public loginVO login(String name, String email, String captcha, HttpSession session) {
        // 校验验证码
        String sessionCaptcha = (String) session.getAttribute("captcha");
        String debug = String.valueOf(redisUtil.get("debug"));
        if (Objects.equals(redisUtil.get("debug"), null)){
            if (!captcha.equalsIgnoreCase(sessionCaptcha)) {
                return loginVO.fail("验证码错误，请重新输入",null);
            }
        }
        // 查询用户
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        Users user = userMapper.selectOne(wrapper);
        if( user == null){
            return loginVO.fail("用户名错误，请重新输入",null);
        }
        if (!user.getEmail().equals(email)){
            return loginVO.fail("邮箱错误，请重新输入",null);
        }
        //登录成功之后，将用户信息存入satoke
        StpUtil.login(user.getId());
        //登录成功之后，将用户信息存入session
        session.setAttribute("user", user);

        //查询用户角色
        List<UserRole> userRole =new ArrayList<>();
         userRole = userRoleMapper.selectList(new LambdaQueryWrapper<UserRole>()
                 .eq(UserRole::getUserId, user.getId()));

        if(userRole == null || userRole.isEmpty()){
            return loginVO.fail("角色未配置，请联系管理员",null);
        }
        List<Long> roleIds = userRole.stream().map(UserRole::getRoleId).collect(Collectors.toList());
        session.setAttribute("roleId",roleIds);
//        session.setAttribute("roleId",);
        return loginVO.success("登录成功");
    }
    public int delete(Long id){
        return userMapper.deleteByName(id);
    }
    public void saveUser(Users user){
        userMapper.insert(user);
    };
    public void deleteuser(long id){
        
    };
    public List<UserVO> selectByName(String id){
        return userMapper.selectByName(id);
    }
}

